package com.zhentao.controller;

import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.pojo.activity.Activity;
import com.zhentao.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author: 桑田
 **/
@RestController
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    //列表
    @GetMapping("/list")
    public ResponseResult list() {
        return ResponseResult.okResult(activityService.list());
    }
    //新增
    @PostMapping("/add")
    public ResponseResult add(@RequestBody Activity activity) {
        return ResponseResult.okResult(activityService.save(activity));
    }
    //修改
    @PostMapping("/update")
    public ResponseResult update(@RequestBody Activity activity) {
        return ResponseResult.okResult(activityService.updateById(activity));
    }
    //删除
    @DeleteMapping("/delete")
    public ResponseResult delete(Integer id) {
        return ResponseResult.okResult(activityService.removeById(id));
    }
    //统计新用户
    @PostMapping("/newUser")
    public ResponseResult newUser(Integer id) {
        return ResponseResult.okResult(activityService.getById(id).getNewUser());
    }
    //图片上传
    @PostMapping("/upload")
    public ResponseResult upload(@RequestParam("file") MultipartFile file) throws IOException {
        return activityService.upload(file);
    }
    //根据活动名称模糊查询
    @PostMapping("/findByName")
    public ResponseResult findByName(@RequestParam("name") String name) {
        return ResponseResult.okResult(
                activityService.lambdaQuery()
                        .like(Activity::getName, name)
                        .list()
        );
    }


}
